package com.multipledatasource.config;

/**
 *数据源切换类
 *      构建一个DataSourceEnum容器，并提供了向其中设置和获取DataSorceEnum的方法
 */
public class DataSourceContextHolder {

    private static final ThreadLocal<DataSourceEnum> contextHolder = new ThreadLocal<DataSourceEnum>(){
        @Override
        protected DataSourceEnum initialValue() {
            return DataSourceEnum.USER;//初始为用户数据源
        }
    };

    public static void setDatabaseType(DataSourceEnum type){
        contextHolder.set(type);
    }
    public static DataSourceEnum getDataSourceType(){
        return contextHolder.get();
    }
    public static void clearDataSourceType(){
        contextHolder.remove();
    }
}
